package com.jgc.locks;

import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2022/3/22.
 */
public class Lock8Demo {
    public static void main(String[] args) throws Exception {
//    test01();
//    test02();
//    test03();
        test04();
}
public static void test01() throws InterruptedException {
    Phone phone = new Phone();
    new Thread(()->{
        try {
            phone.sendEmail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    },"a").start();
    TimeUnit.MICROSECONDS.sleep(200);
    new Thread(()->{
        phone.sendSMS();// 即使也是先打印sendeimal
    },"b").start();
}
    public static void test02() throws InterruptedException {
        Phone phone = new Phone();
        new Thread(()->{
            try {
                phone.sendEmail();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"a").start();
        TimeUnit.MICROSECONDS.sleep(200);
        new Thread(()->{
            phone.hello();//先打印hello
        },"b").start();
    }
    public static void test03() throws InterruptedException {
        Phone phoneA = new Phone();
        Phone phoneB = new Phone();
        new Thread(()->{
            try {
                phoneA.sendEmail();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"a").start();
        TimeUnit.MICROSECONDS.sleep(200);
        new Thread(()->{
            phoneB.sendSMS();
        },"b").start();
    }
    public static void test04() throws InterruptedException {
        Phone phone = new Phone();
        new Thread(()->{
            try {
                Phone.sendEmailEx();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"a").start();
        TimeUnit.MICROSECONDS.sleep(200);
        new Thread(()->{
            Phone.sendSMSEx();
        },"b").start();
    }

    public static void test05() throws InterruptedException {
        Phone phoneA = new Phone();
        Phone phoneB = new Phone();
        phoneA.sendSMSEx();
        new Thread(()->{
            try {
                Phone.sendEmailEx();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"a").start();
        TimeUnit.MICROSECONDS.sleep(200);
        new Thread(()->{
            phoneA.sendSMSEx();
        },"b").start();
    }



}
class Phone{
    public synchronized void sendEmail() throws Exception {
        TimeUnit.SECONDS.sleep(3);
        System.out.println("---------sendEmail");
    }
    public synchronized void sendSMS(){
        System.out.println("---------sendSMS");
    }
    public void hello(){
        System.out.println("hello");
    }

    public static synchronized void sendEmailEx() throws Exception {
        TimeUnit.SECONDS.sleep(3);
        System.out.println("---------sendEmail");
    }
    public static synchronized void sendSMSEx(){
        System.out.println("---------sendSMS");
    }
}
